package hotel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class SettaStanze extends JFrame implements ActionListener{
	ImageIcon iconax=new ImageIcon("Risorse/x4.png");
	private int numStanze;
	private JPanel pannello;
	private JScrollPane scroll;
	private JTextField[] stanze;
	private Salvataggio g;
	private JLabel numero,posti,matrimoniale;
	private String[] clienti={"1","2","3","4"};
	private String[] sino={"NO","SI"};
	private JComboBox[] numeroPosti;
	private JComboBox[] mat;
	private JButton inizializza;
	private Database d;
	
	private void creaLeTabelle() {
		try {
			d.iniz();
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}
	
	public void salva(){
		try {
			FileOutputStream out = new FileOutputStream("Risorse/fileSalvataggio.gtH");
			ObjectOutputStream oout = new ObjectOutputStream(out);
			oout.writeObject(g.getNome());
			oout.writeObject(Integer.valueOf(g.getNumCamere()));
			oout.flush();
			creaLeTabelle();
			this.dispose();
		} catch (Exception exc){
		}
	}
	public SettaStanze(Salvataggio gestione,Database d){
		g=gestione;
		this.d=d;
		numStanze=g.getNumCamere();
		this.setSize(600,500);
		this.setLocation(350,180);
		this.setResizable(false);
		this.setTitle("Settaggio Stanze");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		numero=new JLabel("Numero Stanza");
		posti=new JLabel("Numero Posti");
		matrimoniale=new JLabel("Matrimoniale");
		pannello=new JPanel(new GridLayout(0,3));
		scroll=new JScrollPane(pannello);
		this.add(scroll);
		stanze=new JTextField[numStanze];
		numeroPosti=new JComboBox[numStanze];
		mat=new JComboBox[numStanze];
		pannello.add(numero);
		pannello.add(posti);
		pannello.add(matrimoniale);
		for(int i=0;i<numStanze;i++){
			stanze[i]=new JTextField();
			stanze[i].addActionListener(this);
			pannello.add(stanze[i]);
			numeroPosti[i]=new JComboBox(clienti);
			numeroPosti[i].setActionCommand("" + i);
			numeroPosti[i].addActionListener(this);
			pannello.add(numeroPosti[i]);
			mat[i]=new JComboBox(sino);
			mat[i].setVisible(false);
			pannello.add(mat[i]);
		}
		inizializza=new JButton("Inizializza!");
		inizializza.addActionListener(this);
		pannello.add(new JLabel());
		pannello.add(new JLabel());
		pannello.add(inizializza);
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e){
		   if( e.getSource() instanceof JComboBox) {
		       int c=Integer.valueOf(e.getActionCommand());
		       if(Integer.valueOf((String) numeroPosti[c].getSelectedItem())>1)
					mat[c].setVisible(true);
		       else
					mat[c].setVisible(false);
		   }else if(e.getSource()==inizializza){
			   if(controlla()){
				   salva();

				   popolaTabellaStanze();

				   GraficaHotel gra;
				try {
					gra = new GraficaHotel(g,d);
					gra.setVisible(true);
				} catch (SQLException | IOException e1) {
					e1.printStackTrace();
				}
			   }else{
				   JOptionPane.showMessageDialog(null, "Completare tutti i campi!",
							"ERRORE", JOptionPane.ERROR_MESSAGE,iconax);
				   this.setVisible(true);
			   }
		   }
		}

	private void popolaTabellaStanze() {
		try {
			boolean flag;
			for(int i=0;i<numStanze;i++){
				flag=false;
				if(mat[i].getSelectedItem().equals("SI"))
					flag=true;
				if(!d.query("INSERT INTO CAMERE(numero,numeroposti,numpostioccupati,matrimoniale,stato)" +
						" VALUES('"+stanze[i].getText()+"',"+numeroPosti[i].getSelectedItem()+
						",0,"+flag+",'DISPONIBILE')"))
					JOptionPane.showMessageDialog(null, "Stanza "+stanze[i].getText()+" gia' presente nel Database!",
							"ERRORE", JOptionPane.ERROR_MESSAGE,iconax);
			}
		} catch (SQLException | IOException e) {
			   JOptionPane.showMessageDialog(null,e.getMessage());
		
		}
	}

	private boolean controlla() {
		for(int i=0;i<numStanze;i++){
			if(stanze[i].getText().equals(""))
				return false;
		}return true;
	}
}
